import java.util.Calendar;

public class Judgedate {
    private Calendar starttime;
    private Calendar finishtime;
    private final int weekdays = 7; //表示一周有多少天
    private int minmonth = 1; //最小月份
    private final int maxmonth = 12; //最大月份
    private final int minue = 59; //分钟数进位
    private final int seconds = 60; //秒数
    private final int maxhour = 30; //最大通话小时

    public Judgedate(final Calendar s, final Calendar f) {
        starttime = s;
        finishtime = f;
    }

    //得到当天是本周的第几天
    public int getWeekDay(final int year, final int month, final int day) {
        int weekday;
        int newyear = year;
        int newmonth = month;
        final int numberone = 5;
        final int numbertwo = 4;
        final int numberthree = 100;
        final int numberfour = 400;
        final int numberfive = 3;
        for (; newmonth <= minmonth - 1 || newmonth > maxmonth;) {
            if (newmonth <= minmonth) {
                newyear -= minmonth;
                newmonth += maxmonth;
            } else if (newmonth > maxmonth) {
                newyear += minmonth;
                newmonth -= maxmonth;
            }
        }
        weekday = newyear > 0 ? (numberone + (newyear + 1) + (newyear - 1) / numbertwo - (newyear - 1) / numberthree
                + (newyear - 1) / numberfour) % weekdays : (numberone + newyear
                +
                newyear / numbertwo - newyear / numberthree + newyear / numberfour) % weekdays;
        weekday = newmonth > 2 ? (weekday + 2 * (newmonth + 1) + numberfive * (newmonth + 1) / numberone) % weekdays
                : (weekday + 2 * (newmonth + 2) + numberfive * (newmonth + 2) / numberone) % weekdays;
        if (((newyear % numbertwo == 0 && newyear % numberthree != 0) || newyear % numberfour == 0) && newmonth > 2) {
            weekday = (weekday + 1) % weekdays;
        }
        weekday = (weekday + day) % weekdays;
        return weekday;
    }
    //计算通话时长
    public int judge() {
        float totalsecond;
        int totalminiute;
        boolean issummer1 = false;
        boolean issummer2 = false;
        boolean isaut1 = false;
        boolean isaut2 = false;
        final int numberone = 3;
        final int numbertwo = 11;
        final int numberthree = 8;
        final int numberfour = 59;
        final int numberfive = 10;
        final int numbersix = 26;
        final int numberseven = 7;
        final int numbereight = 31;
        final int numbernine = 24;
        final int numberten = 60;

        int sy = starttime.get(Calendar.YEAR); //年
        int smonth = starttime.get(Calendar.MONTH) + 1; //月
        int sd = starttime.get(Calendar.DATE);       //日
        int sh = starttime.get(Calendar.HOUR_OF_DAY);
        int sm = starttime.get(Calendar.MINUTE);
        int ss = starttime.get(Calendar.SECOND);

        int ey = finishtime.get(Calendar.YEAR); //年
        int emonth = finishtime.get(Calendar.MONTH) + 1; //月
        int ed = finishtime.get(Calendar.DATE);       //日
        int eh = finishtime.get(Calendar.HOUR_OF_DAY);
        int em = finishtime.get(Calendar.MINUTE);
        int es = finishtime.get(Calendar.SECOND);
        //夏令时
        if (smonth == numberone && getWeekDay(sy, smonth, sd) == 0) {
            System.out.println("今天是三月的一个星期天！！");
            if (sh >= 2) {
                System.out.println("拨打电话时间已是夏令时！");
                issummer1 = true;
                sh = sh + 1;
            }
        } else if (smonth == numbertwo && getWeekDay(sy, smonth, sd) == 0 && sd < numberthree) {
            System.out.println("今天是十一月的第一个星期天");
            if (sh == 2 && sm == numberfour && ss == numberfour) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut1 = true;
                sh = 2;
                sm = 0;
                ss = 0;
            } else if (sh >= numberone) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut1 = true;
                sm = sm - numberfour;
                ss = ss - numberfour;
            }
        } //秋令时
        //夏令时
        if (emonth == numberone && getWeekDay(ey, emonth, ed) == 0) {
            if (eh >= 2) {
                System.out.println("结束电话时间已是夏令时");
                issummer2 = true;
                eh = eh + 1;
            }
        } else if (emonth == numbertwo && getWeekDay(ey, emonth, ed) == 0 && ed < numberthree) {
            if (eh == 2 && em == numberfour && es == numberfour) {
                System.out.println("结束电话时间已是秋令时！");
                isaut2 = true;
                eh = 2;
                em = 0;
                es = 0;
            } else if (eh >= numberone) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut2 = true;
                em = em - numberfour;
                es = es - numberfour;
            }
        } //秋令时
        int i;
        for (i = 0; i < numberfive; i++) {
            if (getWeekDay(sy, numberone, numbersix + i) == 0) {
                if (i == 0 || i == 1) {
                    continue;
                } else {
                    break;
                }
            }
        }
        int j;
        for (j = numberseven; j > 0; j--) {
            if (getWeekDay(sy, numberfive, numbereight - j) == 0) {
                break;
            }
        }

        if (ed > sd) { //计算两个日期实际相差时长
            totalsecond = numbernine * seconds * seconds - (sh * seconds * seconds + sm * seconds + ss)
                    + eh * seconds * seconds + em * seconds + es;
        } else {
            totalsecond = (eh * seconds * seconds + em * seconds + es) - (sh * seconds * seconds + sm * seconds + ss);
        }

        if (!isaut1 && isaut2) {
            totalsecond = totalsecond + numberfour + numberfour * numberten;
        }
        if (!issummer1 && issummer2) {
            totalsecond = totalsecond - numberten * numberten;
        }

        float haha = totalsecond % seconds; // 秒数取余，

        if (haha > 0.0) {
            totalminiute = (int) (totalsecond / seconds) + 1;
        } else {
            totalminiute = (int) (totalsecond / seconds);
        }


        //通话时长大于30小时
        int a = (int) (totalminiute / seconds); //计算通话时长，向下取整，一个有多少小时
        if (a > maxhour) { //通话时长大于30小时，返回0，因为计算出来的分钟不会出现0，“不满1分钟按1min计算”原则
            return 0;
        }
        return totalminiute;
    }
}
